package in.abip.chisame.model;

import in.abip.chisame.R;

/**
 * HVAC record.
 * 
 * @author Julot.
 * @since Tuesday, January 25th, 2011.
 */
public class Hvac extends AbstractChannel implements IListItem, ISwitch {
	public Integer minValue = 0;
	public Integer maxValue = 100;
	
	public static Hvac model() {
		return new Hvac();
	}
	
	/** @return string type name */
	public String getTypeName() {
		switch (typeId) {
		case 1: return "Heater";
		case 2: return "Ventilation";
		default: return "Air Conditioner";
		}
	}
	
	/** Implementation of IListItem interface */
	public int getListIconResource() {
		switch (typeId) {
		case 1:
			if (getValue() == 0) return R.drawable.ic_list_hvac_type_1;
			else return R.drawable.ic_list_hvac_type_1_on;
		case 2:
			if (getValue() == 0) return R.drawable.ic_list_hvac_type_2;
			else return R.drawable.ic_list_hvac_type_2_on;
		default:
			if (getValue() == 0) return R.drawable.ic_list_hvac_type_3;
			else return R.drawable.ic_list_hvac_type_3_on;
	    }
	}
	
	/** Implementation of IListItem interface */
	public String getListText() {
		return (name == null) || (name.equals("")) ? getTypeName() : name;
	}
	
	/** @return integer maximum value for seek bar */
	public int getSeekMax() {
		if (hasRange) return maxValue - minValue + 1;
		else return MAX_VALUE;
	}
	
	/** Implementation of ISwitch interface */
	public Integer getValue() {
		if (!hasRange && _value > 0) return getSeekMax();
		return _value;
	}
	
	/** Implementation of ISwitch interface */
	public void setValue(Integer value) {
		_virgin = false;
		if (value > getSeekMax()) _value = getSeekMax();
		else if (value < 0) _value = 0;
		else _value = value;
		_toggleValue = _value;
	}
	
	/** @return integer value for toggle operation */
	private int getToggleValue() {
		if (_toggleValue >= getSeekMax()) return getSeekMax();
		return _toggleValue;
	}
	
	/** Implementation of ISwitch interface */
	public void toggle() {
		_virgin = false;
		if (getValue() > 0) {
			_value = 0;
		}
		else {
			_value = getToggleValue() == 0 ? getSeekMax() : getToggleValue();
		}
	}
	
	/** Implementation of ISwitch interface */
	public String getValueName() {
		return getValueName(getValue());
	}
	
	/** Implementation of ISwitch interface */
	public String getValueName(Integer value) {
		if (_virgin || value.equals(null)) return "Unknown";
		if (value == 0) {
			if (typeId == 2) return "Open";
			return "Off";
		}
		if (typeId == 2) return "Closed";
		if (hasRange) return (value + minValue - 1) + "\u00B0C";
		return "On";
	}
	
	/**
	 * Get command to set value of the device.
	 * @return String the command.
	 */
	public String getRunCmd(Integer value) {
		return Integer.toString(100 + host).substring(1) + 
			Integer.toString(100 + channel).substring(1) + 
			Integer.toString(value + 1000).substring(1);
	}
}
